package com.example.task1.ui.AllService.hospital;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.HospitalSortBean;
import com.example.task1.http.Api;
import com.example.task1.ui.BaseAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.TimeZone;

import retrofit2.Call;

public class HospitalSortAdapter extends BaseAdapter<HospitalSortBean.RowsDTO> {

    private Context context;
    private List<HospitalSortBean.RowsDTO> mData = new ArrayList<>();
    private TextView tvName;
    private TextView tvMoney;
    private Button btnSave;

    public HospitalSortAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void setData(List<HospitalSortBean.RowsDTO> rows) {
        int size = mData.size();
        mData.addAll(rows);
        notifyItemInserted(size);
    }

    @Override
    protected int getLayout() {
        return R.layout.hostpitalsortlist;
    }

    @Override
    protected Context getContext() {
        return null;
    }

    @Override
    protected List<HospitalSortBean.RowsDTO> getData() {
        return mData;
    }

    @Override
    protected void onBind(myHolder holder, HospitalSortBean.RowsDTO date) {
            tvMoney.setText(date.getMoney()+"");
            tvName.setText(date.getCategoryName());
            btnSave.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("categoryId",MyApplication.Hospital.hospitalDate.getId());
                    map.put("money",date.getMoney());
                    map.put("patientName",MyApplication.Hospital.userDate.getName());
                    map.put("type",date.getType());
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
                    map.put("reserveTime",simpleDateFormat.format(new Date()));
                    Call<ApiResp> apiRespCall = Api.api().post(MyApplication.TOKEN, "/prod-api/api/hospital",Api.getBody(map));
                    Api.send(apiRespCall, ApiResp.class, new CallBack<ApiResp>() {
                        @Override
                        public void success(ApiResp data) {
                            MyApplication.show("预约成功");
                            ((HospitalDetailsActivity)context).finish();
                        }

                        @Override
                        public void error(String msg) {

                        }
                    });
                }
            });
    }

    @Override
    protected void holder(View itemView) {
        initView(itemView);
    }

    private void initView(View view) {
        tvName = (TextView) view.findViewById(R.id.tv_name);
        tvMoney = (TextView) view.findViewById(R.id.tv_money);
        btnSave = (Button) view.findViewById(R.id.btn_save);
    }
}
